// pages/caseSrc/caseDetail/caseDetail.js
import request from '../../../utils/config.js';
import formatDate from '../../../utils/util.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    caseId:'',
    detailData: '',
    isapply: '',
    showForm: false,
    viewpointAdv: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.caseDetailInfo(options.caseId)
    this.setData({
      caseId: options.caseId,
      isapply: options.isapply
    })
  },

  caseDetailInfo: function(id) {
    let _this = this;
    let headerInfo = {
      pageNum: '',
      pageSize: '',
      sessionId: app.globalData.sessionId,
    }, bizInfo = {
      caseId: id
    };
    wx.showLoading({ title: ' ', mask: true })
    request.requestLogin_api("queryCaseInfoDetail", headerInfo, bizInfo, (obj) => {
      wx.hideLoading();
      console.log('obj=',obj)
      if (obj.data.isSuccess == 'T') {
        let temp = JSON.parse(obj.data.body)
        console.log('temp', temp)
        temp.caseAmt = formatDate.filterFormatAmt(temp.caseAmt * 1 / 10000);
        temp.status = formatDate.investStatusFilter(temp.status);
        temp.caseTypeId = formatDate.filterCaseTypeId(temp.caseTypeId)

        let address = [];
        temp.caseLbsInfos.forEach(function (y) {
          let str = '';
          if (y.province) {
            str = y.province
          }
          if (y.city) {
            str += ' ' + y.city
          }
          if (y.country) {
            str += ' ' + y.country
          }
          address.push(str)
        })
        address = address.toString();
        if (!address) {
          address = '暂无'
        }
        temp.address = address;

        console.log('temp', temp)
        _this.setData({
          detailData: temp
        })
      } else if (obj.data.isSuccess == 'F') {
        wx.showToast({
          title: obj.data.errorMsg,
          icon: 'none',
          duration:1000
        })
      }
    }, (obj) => {
      wx.hideLoading();
      console.log("获取案件列表失败==", obj)
      wx.showModal({
        title: "加载失败",
        content: '请尝试下拉界面重新加载',
        confirmText: '确定',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
          } else if (res.cancel) {
          }
        }
      })
    })
  },

  apply: function() {
    if (this.data.isapply == 'Y'){
      return;
    }
    this.setData({
      showForm: true
    })
  },
  bindTextAreaBlur: function (e) {
    console.log(e.detail.value)
    this.setData({
      viewpointAdv: e.detail.value
    })
  }, 
  next: function() {
    if (this.data.viewpointAdv) {
      var headerInfo = {
        pageNum : '',
        pageSize : '',
        sessionId: app.globalData.sessionId
      }, bizInfo = {
        caseId: this.data.caseId,
        memoContent: { "viewpointAdv": this.data.viewpointAdv}
      };
      wx.showLoading({ title: ' ', mask: true })
      console.log(bizInfo)
      request.requestLogin_api("submitCaseOrder", headerInfo, bizInfo, (obj) => {//SUCCESS
        // console.log('提交成功===',obj)
        wx.hideLoading()
        if (obj.data.isSuccess == 'T') {
            wx.showToast({
              title: '提交成功',
              icon: 'success',
              mask: true
            })
            setTimeout(function () {
              wx.navigateTo({
                url: '../caseSrc',
              })
            }, 1000)
          } 
      }, function (data) {
        wx.hideLoading()
        wx.showToast({
          title: '提交失败',
          icon: 'none'
        })
      });
    } else {
      wx.showToast({
        title: '请填写案件分析方案',
        icon: 'none',
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})